package com.situ.shoplookweb.controller;

import com.situ.shoplook.common.model.Category;
import com.situ.shoplook.common.model.GoodsInfo;
import com.situ.shoplook.common.service.CategoryService;
import com.situ.shoplook.common.service.GoodsInfoService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
 * @Author xinchen
 * @Date: 2024/09/04/ 10:10
 * @Description
 */
@Controller
public class IndexController {

    private CategoryService categoryService;

    @Autowired
    private GoodsInfoService goodsInfoService;

    @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @GetMapping({"/index", "/", "/index.html"})
    public String indexPage(HttpServletRequest request){
        List<Category> categories = categoryService.findTree().getChildren();
        if(CollectionUtils.isEmpty(categories)){
            throw new RuntimeException("没有分类数据");
        }
        request.setAttribute("categories", categories);//分类数据
        /**
         * 热销数据查询
         */
        List<GoodsInfo> goodsInfos =this.goodsInfoService.findByHotGoodses();
        request.setAttribute("hotGoodses",goodsInfos);
        return "mall/index";
    }
}
